There are many traps and pitfalls in Java. Things which looks obvious at a glance may be tricky. Take a look how is it possible that 4+1=15 or how to read from non-existent file.
Slides from 119. WroclawJUG
2. Ramazan wanted to write "You change
the topic every time you!run out of
arguments" (sounds familiar enough) but
what Emine read was, "You change the
topic every time!they are fucking
you" (sounds familiar too.)
Source: Jesus Diaz, A Cellphone's Missing Dot Kills Two People, Puts Three More in Jail (2008)
Retrieved from: https://gizmodo.com/382026/a-cellphones-missing-dot-kills-two-people-puts-three-more-in-jail
3. When using!
LOWER_CASE_WITH_DASHES!or!
LOWER_CASE_WITH_UNDERSCORES!
as FieldNamingPolicy it could happen that
certain fields won't get serialized/
deserialized.
I've seen that behavior on devices with a
turkish (tr-TR) locale.!
Source: Sebastian Clan, Fix issues if runing in an environment with a Turkish locale (2015)
Retrieved from: https://github.com/google/gson/pull/652/files
4. Dotted and dotless I
Lowercase Uppercase
I
!
I
i
i
ı
English
Turkish
Turkish
dotted
dotless
5. Casing done right
• Default (or specific) locale for UI
• ROOT locale for machine to machine communication like JSON keys or HTTP headers
value.toUpperCase(Locale.ROOT);
6. Casing depends on context
• Dotted and dotless I in Turkish: !i, Iı
• Final and non-final small Greek sigmas with single capital letter: ", #, "
• Retained dot in a lowercase i and j when followed by accents in Lithuanian: i #$
Unicode casing: https://unicode.org/Public/UNIDATA/
SpecialCasing.txt
11. Number pitfalls
x == -x && x != 0
int x = Integer.MIN_VALUE;
x != x
float x = Float.NaN;
x = x + y;
x += y;
short x = 1;
int y = 2;
12. The missing days
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime()); //Sat Oct 06 12:25:24 CEST 2018
calendar.add(YEAR, -436);
System.out.println(calendar.getTime()); //Sat Oct 16 12:25:24 CET 1582
19. Volatile
private static boolean isReady;
public static void main(String[] args) throws Exception {
new Thread(() -> {
while (!isReady);
System.out.println("Ready!");
}).start();
Thread.sleep(1000);
isReady = true;
}
20. REFERENCES
• Java Puzzlers
• Edge Cases to Keep in Mind. Part 1 – Text
• Edge Cases to Keep in Mind. Part 2 – Files
• Unicode casing
• Java Magic. Part 4: sun.misc.Unsafe